const birdDom = document.querySelector(".bird")
const birdStyles = getComputedStyle(birdDom)
const birdWidth = parseFloat(birdStyles.width)
const birdHeight = parseFloat(birdStyles.height);
const birdTop = parseFloat(birdStyles.top);
const birdLeft = parseFloat(birdStyles.left);
const gameDom = document.querySelector(".game");
const gameHeight = gameDom.clientHeight;


class Bird extends Rectangle {
    constructor() {
        super(birdWidth, birdHeight, birdLeft, birdTop, 0, 0, birdDom)
        this.g = 1500 //向下的加速度，单位：像素/秒²
        //最大的y坐标
        this.maxY = gameHeight - landHeight - birdHeight
        this.swingStatus = 1
        this.timer = null
    }

    //重新move方法
    //匀速速度= 加速度 * 时间
    move(duration) {

        // move的方法需要前面，因为还有向上的速度
        super.move(duration)
        this.ySpeed += this.g * duration 

    }

    //开始煽动翅膀
    startSwing() {
        if (this.timer) {
            return
        }

        this.timer = setInterval(() => {
            this.swingStatus = (this.swingStatus % 3) + 1
            // console.log(this.swingStatus);
            this.dom.className = `bird swing${this.swingStatus}`;
        }, 200)
    }

    //停止煽动翅膀
    stopSwing() {
        clearInterval(this.timer);
        this.timer = null;
    }


    onmove() {
        //控制边界值
        if (this.top < 0) {
            this.top = 0
        } else if (this.top > this.maxY) {
            this.top = this.maxY
        }
    }


    //按下空格，给一个向上的速度
    jump() {
        this.ySpeed = -450
    }


}



// const bird = new Bird()

// //移动
// setInterval(() => {
//     bird.move(16 / 1000)
// }, 16)


// //煽动翅膀
// bird.startSwing()








